﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string FK_I_Add(FK_I model, int ACID);


        [OperationContract]
        string FK_I_Update(FK_I model, int ACID);


        [OperationContract]
        string FK_IByiid_Delete(int iid, int ACID);


        [OperationContract]
        FK_I FK_IByiid_Get(int iid, int ACID);


        [OperationContract]
        List<FK_I> FK_I_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加FK_I表数据
        /// <summary>
        /// 添加FK_I
        /// </summary>
        /// <param name="model">FK_I实体</param>
        /// <returns>空或错误</returns>
        public string FK_I_Add(FK_I model, int ACID)
        {
            if (model.iid == null)
            {
                return "『付款单付入明细ID』不能为空！";
            }
            if (model.zftypeid == null)
            {
                return "『支付方式ID』不能为空！";
            }
            if (model.jine == null)
            {
                return "『付入金额』不能为空！";
            }
            if (model.bankid == null)
            {
                return "『银行帐号id』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『付款单号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK_I.Count(p => p.iid == model.iid) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.FK_I.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加FK_I表数据事物用重构
        /// <summary>
        /// 添加FK_I事物用重构
        /// </summary>
        /// <param name="model">FK_I实体</param>
        /// <returns>空或错误</returns>
        private string FK_I_Add(FK_I model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {

            if (model.zftypeid == null)
            {
                return "『支付方式ID』不能为空！";
            }
            if (model.jine == null)
            {
                return "『支付金额』不能为空！";
            }
            if (model.bankid == null)
            {
                return "『银行帐号id』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『付款单号』不能为空！";
            }
            if (ctx.FK_I.Count(p => p.iid == model.iid) > 0)
            {
                return "主键已存在！";
            }
            ctx.FK_I.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改FK_I表数据
        /// <summary>
        /// 修改FK_I
        /// </summary>
        /// <param name="model">FK_I实体</param>
        /// <returns>空或错误</returns>
        public string FK_I_Update(FK_I model, int ACID)
        {
            if (model.iid == null)
            {
                return "『付款单付入明细ID』不能为空！";
            }
            if (model.zftypeid == null)
            {
                return "『支付方式ID』不能为空！";
            }
            if (model.jine == null)
            {
                return "『付入金额』不能为空！";
            }
            if (model.bankid == null)
            {
                return "『银行帐号id』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『付款单号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK_I.Count(p => p.iid == model.iid) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    FK_I FK_Imodel = ctx.FK_I.Single(p => p.iid == model.iid);
                    if (model.iid != null)
                    {
                        FK_Imodel.iid = model.iid;
                    }
                    if (!string.IsNullOrEmpty(model.pid))
                    {
                        FK_Imodel.pid = model.pid;
                    }
                    if (model.bankid != null)
                    {
                        FK_Imodel.bankid = model.bankid;
                    }
                    if (model.jine != null)
                    {
                        FK_Imodel.jine = model.jine;
                    }
                    if (model.zftypeid != null)
                    {
                        FK_Imodel.zftypeid = model.zftypeid;
                    }
                    if (model.fkDate != null)
                    {
                        FK_Imodel.fkDate = model.fkDate;
                    }
                    if (!string.IsNullOrEmpty(model.remark))
                    {
                        FK_Imodel.remark = model.remark;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除FK_I表Byiid数据
        /// <summary>
        /// 删除FK_IByiid
        /// </summary>
        /// <param name="iid">付款单付入明细ID</param>
        /// <returns>空或错误</returns>
        public string FK_IByiid_Delete(int iid, int ACID)
        {
            if (iid == null)
            {
                return "『付款单付入明细ID』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK_I.Count(p => p.iid == iid) != 1)
                    {
                        return null;
                    }
                    FK_I model = ctx.FK_I.Single(p => p.iid == iid);
                    ctx.FK_I.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除FK_I表Byiid数据[事物用重构]
        /// <summary>
        /// 删除FK_IByiid[事物用重构]
        /// </summary>
        /// <param name="iid">付款单付入明细ID</param>
        /// <returns>空或错误</returns>
        private string FK_IByiid_Delete(int iid, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (iid == null)
            {
                return "『付款单付入明细ID』不能为空！";
            }
            if (ctx.FK_I.Count(p => p.iid == iid) != 1)
            {
                return null;
            }
            FK_I model = ctx.FK_I.Single(p => p.iid == iid);
            ctx.FK_I.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取FK_I表Byiid数据
        /// <summary>
        /// 获取FK_IByiid
        /// </summary>
        /// <param name="iid">付款单付入明细ID</param>
        /// <returns>当前表</returns>
        public FK_I FK_IByiid_Get(int iid, int ACID)
        {
            if (iid == null)
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK_I.Count(p => p.iid == iid) != 1)
                    {
                        return null;
                    }
                    return ctx.FK_I.SingleOrDefault(p => p.iid == iid);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取FK_I表数据
        /// <summary>
        /// 获取FK_I
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK_I></returns>
        public List<FK_I> FK_I_Get(Expression<Func<FK_I, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK_I.ToList();
                        }
                        else
                        {
                            return ctx.FK_I.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK_I.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.FK_I.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取FK_I表数据
        /// <summary>
        /// 获取FK_I
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK_I></returns>
        public List<FK_I> FK_I_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return FK_I_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取FK_I表数据计数
        /// <summary>
        /// 获取FK_I表数据计数
        /// </summary>
        /// <returns>List<FK_I></returns>
        public int FK_I_GetCount(Expression<Func<FK_I, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.FK_I.Count();

                    }
                    else
                    {
                        return ctx.FK_I.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取FK_I分页表数据
        /// <summary>
        /// 获取FK_I分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK_I></returns>
        public List<FK_I> FK_I_Getpage(int pageIndex, int pageSize, Expression<Func<FK_I, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK_I.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.FK_I.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK_I.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.FK_I.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改FK_I表数据
        /// <summary>
        /// 批量修改FK_I
        /// </summary>
        /// <param name="predicate">Expression<Func<FK_I, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<FK_I,FK_I>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int FK_I_batchUpdate(Expression<Func<FK_I, bool>> predicate, Expression<Func<FK_I, FK_I>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.FK_I.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改FK_I表数据事物用重构
        /// <summary>
        /// 批量修改FK_I事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<FK_I, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<FK_I,FK_I>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int FK_I_batchUpdate(Expression<Func<FK_I, bool>> predicate, Expression<Func<FK_I, FK_I>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.FK_I.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除FK_I表数据
        /// <summary>
        /// 批量删除FK_I
        /// </summary>
        /// <param name="predicate">Expression<Func<FK_I, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int FK_I_batchDel(Expression<Func<FK_I, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.FK_I.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除FK_I表数据事物用重构
        /// <summary>
        /// 批量删除FK_I事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<FK_I, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int FK_I_batchDel(Expression<Func<FK_I, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.FK_I.Delete(predicate);
            return ok;
        }
        #endregion


    }
}

